package com.yantong.service.impl;

import com.yantong.mapper.CentreCategoryMapper;
import com.yantong.pojo.CategoryExample;
import com.yantong.pojo.vo.CentreCategoryVo;
import com.yantong.service.CentreCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Service实现类
 *
 * @author Kyle
 * @email 572001183@qq.com
 * @date 2017-08-21 15:32:31
 */
@Service
public class CentreCategoryServiceImpl implements CentreCategoryService {
    private static final Logger logger = LoggerFactory.getLogger(CentreCategoryServiceImpl.class);

    @Autowired
    private CentreCategoryMapper centreCategoryMapper;

    @Override
    public List<CentreCategoryVo> listCategorys() {
        List<CentreCategoryVo> categoryList = new ArrayList<>();

        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(1);
        //分类排序
        example.setOrderByClause("displayorder asc");
        categoryList = centreCategoryMapper.selectByExample(example);
//		Long total = centreCategoryMapper.countByExample(example);
//		if (total >0) {
//			categoryList = centreCategoryMapper.selectByExample(example);
//			for (Category vo : categoryList) {
//				CategoryExample example1 = new CategoryExample();
//				CategoryExample.Criteria criteria1 = example1.createCriteria();
//				criteria1.andParentidEqualTo(vo.getId());
//				vo.setCategoryList(centreCategoryMapper.selectByExample(example1));
//			}
//        }
        return categoryList;
    }

    @Override
    public int save(CentreCategoryVo obj) {
        if (obj == null) {
            return 0;
        }
        return centreCategoryMapper.insertSelective(obj);
    }

    @Override
    public CentreCategoryVo getByKey(Long id) {
        if (id == null) {
            return null;
        }
        return centreCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(CentreCategoryVo obj) {
        if (obj == null || obj.getId() == null) {
            return 0;
        }
        if (obj.getEnabled() == null || obj.getEnabled() !=1) {
            obj.setEnabled(0);
        }
        return centreCategoryMapper.updateByPrimaryKeySelective(obj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(Long id) {
        if (id == null) {
            return 0;
        }
        CentreCategoryVo obj = new CentreCategoryVo();
        obj.setId(id);
        obj.setStatus(0);

        centreCategoryMapper.delByParentId(id);
        return centreCategoryMapper.updateByPrimaryKeySelective(obj);
    }

    @Override
    public List<CentreCategoryVo> listCategorys(Long parentId) {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentidEqualTo(parentId);
        criteria.andStatusEqualTo(1);
        //分类排序
        example.setOrderByClause("displayorder asc");
        return centreCategoryMapper.selectByExample(example);
    }

    @Override
    public List<CentreCategoryVo> listCategorys(Long parentId, Integer type) {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentidEqualTo(parentId)
                .andStatusEqualTo(1);
        //分类排序
        example.setOrderByClause("displayorder asc");
        return centreCategoryMapper.selectByExample(example);
    }

    @Override
    public Long selectByName(String name) {
        Long id = centreCategoryMapper.selectByName(name);
        return id == null ? 0 : id;
    }

    @Override
    public Long selectByNameSon(Map map) {
        return centreCategoryMapper.selectByNameSon(map);
    }


    @Override
    public CentreCategoryVo selectCategoryByName(Map map) {
        return centreCategoryMapper.selectCategoryByName(map);
    }

    @Override
    public List<CentreCategoryVo> allCategorys(Long parentId) {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentidEqualTo(parentId)
                .andStatusEqualTo(1);
        //分类排序
        example.setOrderByClause("displayorder asc");
        List<CentreCategoryVo> entityList = centreCategoryMapper.selectByExample(example);
        if(null != entityList && entityList.size() > 0){
            for(CentreCategoryVo categoryVo : entityList){
                example = new CategoryExample();
                criteria = example.createCriteria();
                criteria.andParentidEqualTo(categoryVo.getId())
                        .andStatusEqualTo(1);
                example.setOrderByClause("displayorder asc");
                List<CentreCategoryVo> childList = centreCategoryMapper.selectByExample(example);
                categoryVo.setCategoryList(childList);
            }
        }

        return entityList;
    }
}
